package com.qianfeng.xiaxk.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.xiaxk.R;
import com.qianfeng.xiaxk.utils.LoginUtils;

import java.util.ArrayList;

import cn.sharesdk.framework.CustomPlatform;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.ShareCore;
import cn.sharesdk.sina.weibo.SinaWeibo;

@ContentView(R.layout.activity_setting)
public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    @ViewInject(R.id.settng_exit)
    Button exitBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        com.lidroid.xutils.ViewUtils.inject(this);
        exitBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.settng_exit:
                logout();
                break;
        }
    }

    /**
     * 退出当前账号
     * sharedPreference要修改
     * 要往服务器发数据
     * 要往环信服务器发数据
     */
    private void logout() {
        LoginUtils.loginout(this);
        removeCount();
        ;
        //weibo.removeAccount();/////微博退出授权

        String url = "http://api2.xiaxk.com/api/Setting";
        new HttpUtils().send(HttpRequest.HttpMethod.POST, url, getRequestParams(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });


        //此方法为异步方法，退出环信聊天功能
        EMChatManager.getInstance().logout(new EMCallBack() {

            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgress(int progress, String status) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError(int code, String message) {
                // TODO Auto-generated method stub

            }
        });
        startActivity(new Intent(this, LoginActivity.class));
        finish();
    }

    public RequestParams getRequestParams() {
        //userId=12642&type=2&state=1
        RequestParams params = new RequestParams();
        params.addBodyParameter("userId", LoginUtils.getLoginInfo(this).get("vid"));
        params.addBodyParameter("type", "2");
        params.addBodyParameter("state", "1");
        return params;
    }

    public void removeCount() {
        Platform sinaPlatform = ShareSDK.getPlatform(this, SinaWeibo.NAME);
        if (sinaPlatform.isValid()) {            //取消验证
            sinaPlatform.removeAccount();
        }

    }

}